<?php

	// TITLE: Phone AUTO MODEL
	// FILE: scaffolding/model/phone.php
	// AUTHOR: AUTOGEN
	// CREATED: 2015/04/20 07:49:57
	// DESCRIPTION: 
	// REQUIREMENTS: 

	$createPhone = function($con, $phone) {
		$sql = 'INSERT INTO phone (id, number, extension, type_id) VALUES (';
		$sql .= $phone->getId() . ', ';
		$sql .= $phone->getNumber() . ', ';
		$sql .= $phone->getExtension() . ', ';
		$sql .= $phone->getTypeId() . ')';
		$sql .= ')';
		$result = mysqli_query($con, $sql);
		$id = ($result) ? mysqli_insert_id($con) : $result;
		mysqli_close($con);
		return $id;
	};

	$readPhone = function($con, $id) {
		$sql = 'SELECT * FROM phone WHERE id = ' . $id;
		$results = mysqli_query($con, $sql);
		mysqli_close($con);
		if ($results) {
			$result = mysqli_fetch_array($results);
			$phone = new Phone();
			$phone->setId(result[0]);
			$phone->setNumber(result[1]);
			$phone->setExtension(result[2]);
			$phone->setTypeId($dbio->select($readType(result[3])));
		} else {
			$phone = false;
		}
		return $phone;
	};

	$updatePhone = function($con, $phone) {
		$sql = 'UPDATE phone SET ';
		$sql .= 'id = ' . $phone->getId() . ', ';
		$sql .= 'number = ' . $phone->getNumber() . ', ';
		$sql .= 'extension = ' . $phone->getExtension() . ', ';
		$sql .= 'type_id = ' . $phone->getTypeId() ;
		$sql .= 'WHERE id = ' . id;
		$result = mysqli_query($con, $sql);
		mysqli_close($con);
		return $result;
	};

	$deletePhone = function($con, $id) {
		$sql = 'DELETE FROM phone WHERE id = ' . $id;
		$result = mysqli_query($con, $sql);
		mysqli_close($con);
		return $result;
	};

	$listPhone = function($con) {
		$sql = 'SELECT * FROM phone';
		$results = mysqli_query($con, $sql);
		mysqli_close($con);
		if ($results) {
			$phones = array();
			while ($result = mysqli_fetch_array($results)) {
				$phone = new Phone();
				$phone->setId(result[0]);
				$phone->setNumber(result[1]);
				$phone->setExtension(result[2]);
				$phone->setTypeId($dbio->select($readType(result[3])));
				$phones[] = phone;
			}// end while
		} else {
			$phones = false;
		}
		return $phones;
	};

